package com.simulation.web.controller.tech;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageInfo;
import com.simulation.common.core.domain.AjaxResult;
import com.simulation.web.domain.TechTeams;
import com.simulation.web.dto.TaskTeamMemberDTO;
import com.simulation.web.dto.TeamDTO;
import com.simulation.web.mapper.TeamMemberMapper;
import com.simulation.web.vo.TechTeamMembersVO;
import com.simulation.web.vo.TechTeamVO;
import com.simulation.web.service.TeamService;
import com.simulation.common.core.controller.BaseController;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Api("团队管理")
@RestController
@RequestMapping("team")
public class TechTeamController extends BaseController {

    @Resource
    private TeamService teamService;

    @Resource
    private TeamMemberMapper teamMemberMapper;

    /**
     * 根据ID获取团队详情
     *
     * @param teamDTO
     * @return 返回记录，没有返回null
     */
    @GetMapping("getTeamById")
    @ApiOperation("查询团队详情")
    public AjaxResult getTeamById(TeamDTO teamDTO) {
        TechTeamVO teamVO = teamService.getTeamById(teamDTO.getId());
        if (teamVO == null) {
            return AjaxResult.warn("团队不存在");
        }
        return AjaxResult.success(teamVO);
    }

    /**
     * 新增团队（忽略null字段）
     *
     * @param teamDTO 新增的记录
     * @return 返回影响行数
     */
    @PostMapping("insert")
    @ApiOperation("新增团队")
    public AjaxResult insert(@RequestBody TeamDTO teamDTO) {
        TechTeams team = new TechTeams();
        BeanUtil.copyProperties(teamDTO, team);
        teamService.insert(team);

        TechTeamVO teamVO = new TechTeamVO();
        BeanUtil.copyProperties(team, teamVO);
        return AjaxResult.success(teamVO);
    }

    /**
     * 修改团队信息（忽略null字段）
     *
     * @param teamDTO 修改的记录
     * @return 返回影响行数
     */
    @PostMapping("update")
    @ApiOperation("修改团队信息")
    public AjaxResult update(@RequestBody TeamDTO teamDTO) {
        TechTeams team = new TechTeams();
        BeanUtil.copyProperties(teamDTO, team);
        teamService.update(team);

        TechTeamVO teamVO = new TechTeamVO();
        BeanUtil.copyProperties(team, teamVO);
        return AjaxResult.success(teamVO);
    }

    /**
     * 删除团队（软删除）
     *
     * @param teamDTO 待删除的记录
     * @return 返回影响行数
     */
    @PostMapping("delete")
    @ApiOperation("删除团队")
    public AjaxResult delete(@RequestBody TeamDTO teamDTO) {
        TechTeams team = new TechTeams();
        BeanUtil.copyProperties(teamDTO, team);
        int result = teamService.delete(team.getId());
        return AjaxResult.success(result);
    }

    /**
     * 查询指定用户的所有团队
     *
     * @param teamDTO
     * @return 返回团队列表
     */
    @GetMapping("getTeamsByCreateUserId")
    @ApiOperation("查询指定用户的所有团队")
    public AjaxResult getTeamsByLeaderId(TeamDTO teamDTO) {
        List<TechTeamVO> result = new ArrayList<>();
        List<TechTeamVO> teamList = teamService.getTeamsByLeaderId(teamDTO.getCreateUserId());
        if (CollectionUtil.isNotEmpty(teamList)) {
            result.addAll(teamList);
        }
        List<TechTeamVO> teamVOS = teamService.getTeamByTeamUserId(teamDTO.getCreateUserId());
        if (CollectionUtil.isNotEmpty(teamVOS)) {
            result.addAll(teamVOS);
        }
        result = result.stream().distinct().collect(Collectors.toList());
        return AjaxResult.success(result);
    }


    /**
     * 根据ID获取团队详情
     *
     * @param teamId
     * @return 返回记录，没有返回null
     */
    @GetMapping("getTeamUserById")
    @ApiOperation("查询团队用户")
    public AjaxResult getTeamUserById(@RequestParam("teamId") Long teamId) {
        List<TechTeamMembersVO>  teamVO = teamMemberMapper.getTeamUsersById(teamId);
        return AjaxResult.success(teamVO);
    }

    /**
     * 根据ID获取团队详情
     *
     * @param userId
     * @return 返回记录，没有返回null
     */
    @GetMapping("getTeamByuserId")
    @ApiOperation("查询用户所在团队")
    public AjaxResult getTeamByuserId(@RequestParam("userId") Long userId) {
        List<TechTeamVO>  teamVO = teamService.getTeamByTeamUserId(userId);
        return AjaxResult.success(teamVO);
    }

    /**
     * 根据ID获取团队详情
     *
     * @param taskTeamMemberDTO
     * @return 返回记录，没有返回null
     */
    @GetMapping("getTeamPageByuserId")
    @ApiOperation("查询用户所在团队分页")
    public AjaxResult getTeamPageByuserId(TaskTeamMemberDTO taskTeamMemberDTO) {
        PageInfo<TechTeamMembersVO> teamVO = teamService.getTeamPageByuserId(taskTeamMemberDTO);
        return AjaxResult.success(teamVO);
    }
}